/**
 * Copyright (c) 2005 - 2007 FeyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.home.web.common;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * TODO Class or interface comments begin with a single descriptive sentence.
 * 
 * @author Fenqiang.Zhuang
 * @version $Id: JsonMVCUtil.java,v 1.1 2007/11/01 01:55:51 ffzhuang Exp $
 */
public class JsonMVCUtil {
	
	/**
	 * public json object out to Ajax page
	 * 
	 * @param jsonData
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public static void jsonFormalResponse(JSONObject jsonData,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/x-json");
		}

		PrintWriter out = response.getWriter();
		
		JSONObject json = new JSONObject();
		JSONArray jsonItems = new JSONArray();
		jsonItems.put(jsonData);
		json.put("results", jsonItems);
		
		if (scriptTag) {
			out.write(cb + "(");
		}
		
		response.getWriter().print(json);
		
		if (scriptTag) {
			out.write(");");
		}
	}
	
	/**
	 * public json object out to Ajax page
	 * 
	 * @param jsonData
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public static void jsonResponse(JSONObject jsonData,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/x-json");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "([");
		}
		response.getWriter().print(jsonData);
		if (scriptTag) {
			out.write("]);");
		}
	}

	/**
	 * This will create error json data information
	 * 
	 * @param errors
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public static void jsonErrorsResponse(JSONObject errors,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/x-json");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "(");
		}

		System.out.println(errors);

		response
				.getWriter()
				.print(
						"{errors:["
								+ errors
								+ "], 'errorInfo':'Validation Errors, please fix the errors noted'}");
		if (scriptTag) {
			out.write(");");
		}
	}

	/**
	 * Ok information
	 * 
	 * @param okMsg
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public static void jsonOkResponse(String okMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/x-json");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "(");
		}

		response.getWriter().print("{success:true,'info':'" + okMsg + "'}");
		if (scriptTag) {
			out.write(");");
		}
	}

	/**
	 * Failed information
	 * 
	 * @param failMsg
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public static void jsonFailResponse(String failMsg,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/x-json");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "(");
		}

		response.getWriter().print(
				"{success:false,'errorInfo':'" + failMsg + "'}");
		if (scriptTag) {
			out.write(");");
		}
	}
	
	public static void jsonHtmlResponse(String html, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/text");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "(");
		}

		response.getWriter().print(html);
		if (scriptTag) {
			out.write(");");
		}
	}

}
